const rule20110901 = {
    start: 3500,
    rule: [
        {
            min: 0, max: 1500, ratio: 0.03, quick: 0
        },
        {
            min: 1500, max: 4500, ratio: 0.1, quick: 105
        },
        {
            min: 4500, max: 9000, ratio: 0.2, quick: 555
        },
        {
            min: 9000, max: 35000, ratio: 0.25, quick: 1005
        },
        {
            min: 35000, max: 55000, ratio: 0.3, quick: 2755
        },
        {
            min: 55000, max: 80000, ratio: 0.35, quick: 5505
        },
        {
            min: 80000, max: -1, ratio: 0.45, quick: 13505
        }
    ]
};
const rule20181001 = {
    start: 5000,
    rule: [
        {
            min: 0, max: 3000, ratio: 0.03, quick: 0
        },
        {
            min: 3000, max: 12000, ratio: 0.1, quick: 210
        },
        {
            min: 12000, max: 25000, ratio: 0.2, quick: 1410
        },
        {
            min: 25000, max: 35000, ratio: 0.25, quick: 2660
        },
        {
            min: 35000, max: 55000, ratio: 0.3, quick: 4410
        },
        {
            min: 55000, max: 80000, ratio: 0.35, quick: 7160
        },
        {
            min: 80000, max: -1, ratio: 0.45, quick: 15160
        }
    ]
};

function checkInput() {
    //税前月薪
    let salary = $("#salary").val();
    if (!salary) {
        showToast("请输入税前月薪");
        return;
    }
    //社保公积金扣除金额
    let socialSecurity = $("#socialSecurity").val();
    if (!socialSecurity) {
        showToast("请输入社保公积金扣除金额");
        return;
    }

    if (socialSecurity - salary > 0) {
        showToast("输入有误，请重新输入！")
        return;
    }

    calculateResult(salary, socialSecurity)
}

function calculateResult(salary, socialSecurity) {
    const result20110901 = calculate(salary, socialSecurity, rule20110901);
    const result20181001 = calculate(salary, socialSecurity, rule20181001);
    $('#input').hide();
    $('#result').show();
    $('#result').html(
        '<table class="table bordered" style="font-size: 0.6rem">\n' +
        '    <thead>\n' +
        '    <tr>\n' +
        '        <th>详细</th>\n' +
        '        <th>新个税</th>\n' +
        '        <th>老个税</th>\n' +
        '    </tr>\n' +
        '    </thead>\n' +
        '    <tbody>\n' +
        '    <tr>\n' +
        '        <td>税前月薪</td>\n' +
        '        <td>' + result20181001.salary + '元</td>\n' +
        '        <td>' + result20110901.salary + '元</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>社保公积金</td>\n' +
        '        <td>' + result20181001.socialSecurity + '元</td>\n' +
        '        <td>' + result20110901.socialSecurity + '元</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>起征点</td>\n' +
        '        <td>' + result20181001.start.toFixed(2) + '元</td>\n' +
        '        <td>' + result20110901.start.toFixed(2) + '元</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>应纳税所得额</td>\n' +
        '        <td>' + result20181001.pure.toFixed(2) + '元</td>\n' +
        '        <td>' + result20110901.pure.toFixed(2) + '元</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>税率</td>\n' +
        '        <td>' + result20181001.taxRatio * 100 + '%</td>\n' +
        '        <td>' + result20110901.taxRatio * 100 + '%</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>速算扣除数</td>\n' +
        '        <td>' + result20181001.quick.toFixed(2) + '元</td>\n' +
        '        <td>' + result20110901.quick.toFixed(2) + '元</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>应纳税额</td>\n' +
        '        <td>' + result20181001.finalTax.toFixed(2) + '元</td>\n' +
        '        <td>' + result20110901.finalTax.toFixed(2) + '元</td>\n' +
        '    </tr>\n' +
        '    <tr>\n' +
        '        <td>税后月薪</td>\n' +
        '        <td>' + result20181001.finalSalary.toFixed(2) + '元</td>\n' +
        '        <td>' + result20110901.finalSalary.toFixed(2) + '元</td>\n' +
        '    </tr>\n' +
        '    </tbody>\n' +
        '    <tfoot>\n' +
        '    </tfoot>\n' +
        '</table>\n' +
        '<button onclick="reStart()" type="button" class="btn block primary confirmButton">重新计算</button>'
    )
}

function calculate(salary, socialSecurity, ruleObject) {
    //应纳所得税额
    let pure = salary - socialSecurity - ruleObject.start;
    if (pure < 0) {
        pure = 0;
    }

    //速算扣除数
    let quick = 0;
    //税额，实际交税还要减去速算扣除数
    let taxRatio = 0;
    //税额，实际交税还要减去速算扣除数
    let tax = 0;

    if (pure > 0) {
        let taxRule;
        for (let item of ruleObject.rule) {
            if (pure > item.min && (pure <= item.max || item.max === -1)) {
                taxRule = item;
                break
            }
        }
        if (taxRule) {
            quick = taxRule.quick;
            taxRatio = taxRule.ratio;
            tax = taxRatio * pure;
        }
    }

    const finalTax = tax - quick;
    const finalSalary = salary - socialSecurity - finalTax;
    return {
        salary: salary,
        socialSecurity: socialSecurity,
        start: ruleObject.start,
        pure: pure,
        taxRatio: taxRatio,
        tax: tax,
        quick: quick,
        finalTax: finalTax,
        finalSalary: finalSalary
    }
}

function reStart() {
    $('#input').show();
    $('#result').hide();
}

function showToast(message) {
    new $.Display({
        display: 'messager',
    })
        .show(
            {
                content: message,
                placement: 'top',
                autoHide: 2000,
                type: 'error'
            }
        );
}